<?php
namespace App\Logic\Amazon\AdsApi;

use AmazonAdvertisingApi\Client;
class Report
{
    /**
     * 获取listProfiles
     * @param $config
     * @return mixed|null
     */
    public static function listProfiles($config): mixed
    {
        try {
            $client = new Client($config);
            $result = $client->listProfiles();
            $re = self::extractResponse($result);
            if(!$re["status"]) {
                report('list-[账号ID:'.$config['accountId'].",listProfiles error" . $re["info"]);
                return null;
            }
            return $re["info"];
        } catch (\Exception $e) {
            report('list-[账号ID:'.$config['accountId'].",listProfiles error" . $e->getTraceAsString());
            return null;
        }
    }

    /**
     * 创建Report
     * @param $config
     * @param $profileId
     * @param $params
     * @return mixed
     */
    public static function createReport($config,$profileId,$params): mixed
    {
        try {
            $client = new Client($config);
            $client->profileId = $profileId;
            $result = $client->requestOfflineReport($params);
            $re = self::extractResponse($result);
            if(!$re["status"]) {
                report('create-[账号ID:'.$config['accountId'].",createReport error" . $re["info"]);
                return null;
            }
            return $re["info"];
        } catch (\Exception $e) {
            report('create-[账号ID:'.$config['accountId'].",createReport error" . $e->getTraceAsString());
            return null;
        }
    }

    /**
     * 获取报告
     * @param $config
     * @param $profileId
     * @param $reportId
     * @return mixed
     */
    public static function getReport($config,$profileId,$reportId): mixed
    {
        try {
            $client = new Client($config);
            $client->profileId = $profileId;
            $result = $client->getOfflineReport($reportId);
            $re = self::extractResponse($result);
            if(!$re["status"]) {
                report('get-[账号ID:'.$config['accountId'].",getReport error" . $re["info"]);
                return null;
            }
            return $re["info"];
        } catch (\Exception $e) {
            report('get-[账号ID:'.$config['accountId'].",getReport error" . $e->getTraceAsString());
            return null;
        }
    }

    /**
     * 下载报告
     * @param $config
     * @param $profileId
     * @param $location
     * @return mixed
     */
    public static function downloadReport($config,$profileId,$location): mixed
    {
        try {
            $client = new Client($config);
            $client->profileId = $profileId;
            $result = $client->download($location,true);
            $re = self::extractResponse($result);
            if(!$re["status"]) {
                report('parse-[账号ID:'.$config['accountId'].",downloadReport error" . $re["info"]);
                return null;
            }
            return $re["info"];
        } catch (\Exception $e) {
            report('parse-[账号ID:'.$config['accountId'].",downloadReport error" . $e->getTraceAsString());
            return null;
        }
    }

    /**
     * 提取结果【response】
     * @param $result
     * @return array
     */
    public static function extractResponse($result): array
    {
        if(!is_array($result)){
            return ['status' => false,'info' => '返回结果非数组！'];
        }
        if(array_key_exists("success", $result) && $result["success"] &&
            array_key_exists("code", $result) && $result["code"] == 200) {
            if (array_key_exists("response", $result) && $result["response"]) {
                return ['status' => true,'info' => json_decode($result["response"],true)];
            }
        }
        return ['status' => false,'info' => '提取结果失败：'.':'.json_encode($result).'||'.($result["response"]['detail'] ?? '')];
    }
}
